package com.aoxin.service.impl.course.systemCourse;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.aoxin.mapper.course.systemCourse.AloneCourseDao;
import com.aoxin.mapper.course.systemCourse.SchoolCourseDao;
import com.aoxin.model.course.AloneCourse;
import com.aoxin.model.course.SchoolCourse;
import com.aoxin.service.inter.course.systemCourse.AloneCourseService;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.ThreadLocalUtil;

@Service("AloneCourseService")
public class AloneCourseServiceImpl implements AloneCourseService {

	@Autowired
	private AloneCourseDao aloneCourseDao;
	@Autowired
	private SchoolCourseDao schoolCourseDao;

	/**
	 * 新增单独课程
	 * 
	 * @param aloneCourse
	 * @return
	 */
	@Override
	@Transactional
	public Integer insertAloneCourse(AloneCourse aloneCourse) throws Exception {
		Assert.notNull(aloneCourse, ErrorUtil.COURSE_SYSTEM);
		Assert.notNull(aloneCourse, ErrorUtil.COURSE_TYPE);
		Integer employeeId = ThreadLocalUtil.getEmployee().getEmployeeId();
		String name = ThreadLocalUtil.getEmployee().getName();
		Assert.notNull(employeeId, ErrorUtil.EMPLOYEE_Id);
		Assert.notNull(name, ErrorUtil.EMPLOYEE_Name);
		aloneCourse.setEmployeeId(employeeId);
		aloneCourse.setEmployeeName(name);
		Integer col = aloneCourseDao.checkCourse(aloneCourse);
		if (col == 0) {
			return aloneCourseDao.insertAloneCourse(aloneCourse);

		} else {
			throw new Exception(ErrorUtil.COURSE_CHECK);
		}
	}

	/**
	 * 查询单独课程
	 * 
	 * @param aloneCourse
	 * @return
	 */
	@Override
	public List<AloneCourse> getAloneCourse(AloneCourse aloneCourse,Integer state) {
	//	Integer schoolId = ThreadLocalUtil.getEmployee().getSchoolId();
	//	aloneCourse.setSchoolId(String.valueOf(schoolId));
		if(state == null){
	    aloneCourse.setState(1);
		}
		List<AloneCourse> list = aloneCourseDao.getAloneCourse(aloneCourse);
		return list;
	}

	/**
	 * 修改单独课程
	 * 
	 * @param aloneCourse
	 * @return
	 */
	@Override
	@Transactional
	public Integer updateAloneCourse(AloneCourse aloneCourse){
		Integer col = aloneCourseDao.checkCourse(aloneCourse);
		if(col > 0){
			throw new IllegalArgumentException(ErrorUtil.COURSE_CHECK);
		}else{
			//判断似乎否绑定了包,绑定了则不能被随意修改
			/*Integer a = schoolCourseDao.IsBindingPackage(aloneCourse.getCourseId());
			if(a > 0){
			throw new IllegalArgumentException(ErrorUtil.BINDINGPACKAGE_COURSE);
			}*/
			//2018-06-12放开课程不能被修改需求
			//同步更新所有学校中的课程
			aloneCourseDao.updateSchoolCourseAll(aloneCourse);
			return aloneCourseDao.updateAloneCourse(aloneCourse);
		}
	}

	@Override
	public List<AloneCourse> getAlonCourseToCoursePageId(Integer coursePageId) {
		return 	this.aloneCourseDao.getAlonCourseToCoursePageId(coursePageId);
	}
	
    //改变状态 
	@Override
	public void updateAloneCourseState(List<Integer> courseIds, Integer state) {
		Assert.notEmpty(courseIds, String.format(ErrorUtil.PARAMETER_NULL, "courseIds"));
		Assert.notNull(state, String.format(ErrorUtil.PARAMETER_NULL, "state"));
		for(Integer courseId : courseIds){
			//判断似乎否绑定了包,绑定了则不能被随意修改
			Integer a = schoolCourseDao.IsBindingPackage(courseId);
			if(a > 0){
			throw new IllegalArgumentException(ErrorUtil.BINDINGPACKAGE_COURSES);
			}
		}
		this.aloneCourseDao.updateStates(courseIds, state);
	}
}
